package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.BedPutBean;
import com.bed.lovewithpatientbed.common.BedPutSearchParams;
import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.core.annocation.LogAnno;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.*;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @ClassName BedController.java
 * @author sunningsheng
 * @version 1.0.0
 * @Description 床位管理Controller
 * @createTime
 */
@Controller
@RequestMapping("/bed")
public class BedController {

    @Autowired
    private BedService bedService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private BedStatusSearvice bedStatusSearvice;

    /**
     * @title
     * @description 床位投放
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "床位投放")
    @RequestMapping("/bedPutDo")
    @ResponseBody
    public String bedPut(BedPut bedPut){
        bedService.bedPut(bedPut);
        return "redirect:bedlist";
    }

    /**
     * @title bedAdd
     * @description 床位新增
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "床位新增")
    @RequestMapping("/bedAddDo")
    @ResponseBody
    public Map bedAdd(Bed bed){
        System.out.println("BedController.bedAdd"+"---->"+bed);
        Map<String,Object> map = new HashMap<>();
        try {
            bedService.bedAdd(bed);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
     * @title bedUpdate
     * @description 床位信息修改
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @RequestMapping("/bedUpdateDo")
    @ResponseBody
    public Map<String,Object> bedUpdate(BedPut bedPut){
        System.out.println("BedController.bedUpdate");
        Map<String,Object> map = new HashMap<>();
        try {
            bedService.bedUpdate(bedPut);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
     * @title bedSelectBybid
     * @description 根据ID查找床位
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "根据ID查找床位")
    @RequestMapping("/bedSelectBybidDo")
    @ResponseBody
    public Map<String,Object> bedSelectBybidDo(Integer bedPutId){
        Map<String,Object> map = new HashMap<>();
        try{
            BedPut bedPut = bedService.bedSelectBybid(bedPutId);
            map.put("bedPut",bedPut);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
     * @title bedSelectWithLimit
     * @description layui分页筛选已投放床位
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "根据ID查找床位或条件筛选")
    @RequestMapping("/bedSelectWithLimit")
    @ResponseBody
    public Map<String,Object> bedSelectWithLimit(   LayUiPageBean pageBean, BedPutSearchParams bedPutSearchParams){

        List<BedPut> bedPutList = bedService.bedSelectWithLimit(pageBean, bedPutSearchParams);

        List<BedPutBean> bedPutBeanList = new ArrayList<>();

        System.out.println(pageBean);
        /**
         * 封装bedPutBean
         */
        if (bedPutList != null){
            for(BedPut bedPut:bedPutList){
                BedPutBean bedPutBean = new BedPutBean();
                bedPutBean.setBedPutId(bedPut.getBedPutId());
                bedPutBean.setBedId(bedPut.getBedId());

                Hospital hospital = hospitalService.selectById(bedPut.getHospitalId());
                Integer addressId = hospital.getAddressId();
                Address address = addressService.getById(addressId);
                String province = address.getProvince();
                bedPutBean.setProvince(province);

                Integer addressId1 = address.getAddressId();
                bedPutBean.setAddressId(addressId1);

                String city = address.getCity();
                bedPutBean.setCity(city);

                String district = address.getDistrict();
                bedPutBean.setDistrict(district);

                String hospitalName = hospital.getHospitalName();
                bedPutBean.setHospitalName(hospitalName);

                Dept dept = deptService.selectById(bedPut.getDeptId());
                bedPutBean.setDeptName(dept.getDeptName());

                bedPutBean.setPutDate(bedPut.getPutDate());

                String statusName = bedStatusSearvice.getStatusName(bedPut.getBedStatusId());
                bedPutBean.setBedStatusName(statusName);

                bedPutBeanList.add(bedPutBean);
            }
        }
        long countRow = bedService.countRow();
        return LayUiUtil.getLayTableResponseMap("success", 0,
                countRow, bedPutBeanList);
    }

    /**
     * @title bedDeleteByPrimary
     * @description 删除床位信息(将状态ID设为2)
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @RequestMapping("/bedDeleteByPrimaryDo")
    @ResponseBody
    public Map<String,Object> bedDeleteByPrimary(Integer[] bedPutIds){
        System.out.println("BedController.bedDeleteByPrimary"+bedPutIds);
        System.out.println("BedController.bedDeleteByPrimary");
        Map<String,Object> map = new HashMap<>();
        try{
            bedService.bedDeleteByPrimary(bedPutIds);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return  map;
    }


    /**
     * @title getProvince
     * @description 获取地址信息
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "获取地址信息")
    @RequestMapping("/getProvince")
    @ResponseBody
    public List<Address> getProvince(){
        System.out.println("BedController.getProvince");
        return bedService.selectProvince();
    }

    /**
     * @title getCity
     * @description 根据省份获取城市
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "根据省份获取城市")
    @RequestMapping("/getCity")
    @ResponseBody
    public List<Address> getCity(String province){
        return bedService.selectCity(province);
    }

    /**
     * @title getDistrict
     * @description 根据城市获取区
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "根据城市获取区")
    @RequestMapping("/getDistrict")
    @ResponseBody
    public List<Address> getDistrict(String city){
        return bedService.selectDistrict(city);
    }

    /**
     * @title getHospital
     * @description 根据区获取医院信息
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @LogAnno(operateType = "根据区获取医院信息")
    @RequestMapping("/getHospital")
    @ResponseBody
    public List<Hospital> getHospital(String district){
        System.out.println("BedController.getHospital"+"--->"+bedService.selectHospital(district));
        return bedService.selectHospital(district);
    }

    /**
     * @title closeBed
     * @description 将待机状态的床位修改为关机
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @RequestMapping("/closeBed")
    @ResponseBody
    public Map<String,Object> closeBed(Integer bedPutId){
        System.out.println("BedController.closeBed"+bedPutId);
        Map<String,Object> map = new HashMap<>();
        try {
            bedService.closeBed(bedPutId);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
         * @title 根据医院id和科室id查询床位
         * @description
         * @author yg
         * @updateTime
         * @throws
         */
    @RequestMapping("HDbed")
    @ResponseBody
    public Map<String,Object> selectByHD(Integer hospitalId,Integer deptId){

        Map<String,Object> map=new HashMap<>();
        BedPutBean bedPutBean=new BedPutBean();
        List<BedPut> bedPutList = bedService.selectByHospitalIdAndDeptId(hospitalId, deptId);
        List<BedPutBean> bedPutBeanList=new ArrayList<>();
        if (bedPutList.size()>0){
            for (BedPut bed:bedPutList
                 ) {
                bedPutBean.setBedId(bed.getBedId());
                bedPutBean.setPutDate(bed.getPutDate());
                String statusName = bedStatusSearvice.getStatusName(bed.getBedStatusId());
                bedPutBean.setBedStatusName(statusName);

                bedPutBeanList.add(bedPutBean);
            }
        }
        long countRow = bedService.countRow();
        map.put("bedPutList",bedPutList);
        return LayUiUtil.getLayTableResponseMap("success",0,countRow,bedPutBeanList);
    }

}
